package com.thanhloyal.exchanger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import com.example.exchanger.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.thanhloyal.exchanger.RateData.Pair;
import com.thanhloyal.exchanger.utils.SchemaHelper;
import com.thanhloyal.exchanger.utils.Utils;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.GetChars;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	TextView currencyName = null;
	EditText inputEdt = null;
	Spinner sourceCurrencySpinner;
	ListView destCurMainListView = null;
	
	HashSet<String> codesSet = null;
	
	private ArrayList<Currency> currenciesList = null;
	
	String [] codeStringArr;
	ArrayList<RateHolder> rateHoldersList = null;
	
	ArrayList<RateData> rateDataList;
	
	ArrayList<RateDataItem> rateItems;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// Initialize variables
		rateDataList = new ArrayList<RateData>();
		rateItems = new ArrayList<RateDataItem>();
		
		
		// Do the decorating works
		// Hide the action bar
		ActionBar actionBar = getSupportActionBar();
		actionBar.hide();
		
		SlidingMenu slidingMenu = new SlidingMenu(this);
		slidingMenu.setMode(SlidingMenu.LEFT);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		slidingMenu.setBehindOffsetRes(R.dimen.sliding_menu_behind_offset);
		slidingMenu.setMenu(R.layout.sliding_menu);
		// Decorating done
		
		destCurMainListView = (ListView)findViewById(R.id.dest_cur_main_listview);
		
		
		Typeface britalicBold = Typeface.createFromAsset(getAssets(), "britalic_bold.TTF");
		// currencyName = (TextView)findViewById(R.id.currency_name);
		// currencyName.setTypeface(font);
		
		inputEdt = (EditText)findViewById(R.id.inputEdt);
		inputEdt.setTypeface(britalicBold);
		
		sourceCurrencySpinner = (Spinner)findViewById(R.id.source_currency_spinner);
		
		currenciesList = new ArrayList<Currency>();
		
		Utils utils = new Utils(this);
		
		
		// Prepare Currencies
		rateItems = utils.getRatesData();
		
		// Log.v("Loyal", "Read: " + rateItems.size() + "pairs");
		
		codesSet = new HashSet<String>();
		for (int i = 0; i < rateItems.size(); i++) {
			RateDataItem item = rateItems.get(i);
			codesSet.add(item.getFirstCode());
			codesSet.add(item.getSecondCode());
		}
		
		
		ArrayList<String> codesSortedList = new ArrayList<String>(codesSet);
		Collections.sort(codesSortedList);
		for (String code : codesSortedList) {
			//Log.v("Loyal", "Code: " + code);
			Currency currency = utils.buildCurrencyFormCode(code);
			if (currency != null) {
				currenciesList.add(currency);
			}
			
		}
		Log.v("Loyal", "Build currency from codesSet: passed in " + codesSet.size()
				+ ". " + currenciesList.size() + " currency were built.");
		
		Collections.sort(currenciesList);
		
		
		//////////////////// Source currency spinner
		CurrencySpinnerAdapter adapter = new CurrencySpinnerAdapter(this,
				R.layout.currency_spinner_item, currenciesList);
		
		sourceCurrencySpinner.setAdapter(adapter);
		
		// Set USD as default source currency
		Currency usd = utils.buildCurrencyFormCode("usd");
		int position = adapter.getPosition(usd);
		sourceCurrencySpinner.setSelection(position);
		
		sourceCurrencySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				TextView tv = (TextView) view.findViewById(R.id.currency_spinner_item_code);
				String scc = tv.getText().toString();
				
				sourceCurrencySelected(scc);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

			
		});
		
		//----- End source currency spinner----//
		
		
		rateHoldersList = new ArrayList<RateHolder>();

		for (int i = 0; i < currenciesList.size(); i++) {
			RateHolder rateHolder = new RateHolder();
			rateHolder.setCurrency(currenciesList.get(i));
			rateHoldersList.add(rateHolder);
		}

		MainCurListViewAdapter mainCurListViewAdapter = new MainCurListViewAdapter(
				this, rateHoldersList);
		mainCurListViewAdapter.setSourceCurrrency(usd);   ///////////////// Notice to change this
		mainCurListViewAdapter.setRatesDataList(rateItems);
		mainCurListViewAdapter.calculateRates();   // this may change to some where more automatic
		mainCurListViewAdapter.setInputAmount(1);
		destCurMainListView.setAdapter(mainCurListViewAdapter);

	}

	
	
	private void sourceCurrencySelected(String selectedCurCode){
		Log.v("Loyal", "Source Currency Spinner: " +selectedCurCode +" selected.");
	}
	
	public void menuButtonClicked(View btn){
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		
		// I disable this menu
		// getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void clearButtonClicked(View btn) {
		inputEdt.setText("");
	}
	
	private Currency getCurrencyFromListByCode(String code, List<Currency> list){
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getCurrencyCode().equalsIgnoreCase(code)) {
				return list.get(i);
			}
		}
		
		return null;
	}
	
	/*
	private void addCurrencies(){
		
		Currency usd = new Currency(R.drawable.flag_us, "USD", "United State Dollar");
		Currency afn = new Currency(R.drawable.flag_afghanistan, "AFN", "Afghanistan Afgani");
		Currency mzn = new Currency(R.drawable.flag_mozambique, "MZN", "Mozambique New Metical");
		Currency all = new Currency(R.drawable.flag_albania, "ALL", "Albanian Lek");
		currenciesList.add(usd);
		currenciesList.add(afn);
		currenciesList.add(mzn);
		currenciesList.add(all);
	}
	*/
}




/*
 /// Test dest list
		
		
		//Log.v("Loyal", "BEGIN BUILDING DATA TEST");
		RateData testData = RateData.getInstance();
		// testData.dumpData();
		
		codesSet = new HashSet<String>();
		ArrayList<String> pairs = testData.pairs;
		for (String pair : pairs) {
			String inCurCodeString = pair.substring(0, pair.indexOf('/'));
			String outCurCodeString = pair.substring(pair.indexOf("/") + 1);
			codesSet.add(inCurCodeString);
			codesSet.add(outCurCodeString);
		}
		
		for (String code : codesSet) {
			//Log.v("Loyal", "Code: " + code);
			Currency currency = buildCurrencyFormCode(code);
			if (currency != null) {
				currenciesList.add(currency);
			}
			
		}
		Log.v("Loyal", "Build currency from codesSet: passed in " + codesSet.size()
				+ ". " + currenciesList.size() + " currency were built.");
		
		Collections.sort(currenciesList);
		
		
		//////////////////// Source currency spinner
		CurrencySpinnerAdapter adapter = new CurrencySpinnerAdapter(this,
				R.layout.currency_spinner_item, currenciesList);
		
		sourceCurrencySpinner.setAdapter(adapter);
		
		// Set USD as default source currency
		Currency usd = buildCurrencyFormCode("usd");
		int position = adapter.getPosition(usd);
		sourceCurrencySpinner.setSelection(position);
		
		sourceCurrencySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				TextView tv = (TextView) view.findViewById(R.id.currency_spinner_item_code);
				String scc = tv.getText().toString();
				
				sourceCurrencySelected(scc);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

			
		});
		
		//----- End source currency spinner----//
		
		rateHoldersList = new ArrayList<RateHolder>();
		
		for (int i = 0; i < currenciesList.size(); i++) {
			RateHolder rateHolder = new RateHolder();
			rateHolder.setAskRate(1);
			rateHolder.setBidRate(1);
			rateHolder.setCurrency(currenciesList.get(i));
			rateHoldersList.add(rateHolder);
		}
		
		MainCurListViewAdapter mainCurListViewAdapter = new MainCurListViewAdapter(
				this, rateHoldersList);
		
		destCurMainListView.setAdapter(mainCurListViewAdapter);
		/// End of test dest list
		
		
 */
